

#include "thread_disp.h"

#include <QTimer>
#include <QTime>
#include <QDebug>
#include <qdatetime.h>

#include <iostream>
#include <fstream>

using namespace std;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include <asm/types.h>
#include "global.h"
#include <opencv2/opencv.hpp>
#include <cv.h>

#define DETECT_DANGER 0

// debug params
short back_25088[25088]={0};
int debug_disp = 0;
int readstate_disp = 1-debug_disp;
// end debug
int box_generate = 0;
int init = 0;

char* vocnames[4] = { "dog",
"man",
"hat",
"car", };
float netout_f[output_length];
int numBoxes = 0;
char name_and_prob[100];
char other_msg[100];
Rect* boxes;

CvFont font;
int car_high = 0;
int camera_disp(int shownum)
{
    int res = 99;
    if(framecontrol[0].STATED == 3){
        res = 0;
        short *netop;

        //read data from chiop
        msync((fbaddr3[framecontrol[0].RX_ID].fbV), output_length*2*sizeof(char), MS_SYNC);
        netop = (short*)fbaddr3[framecontrol[0].RX_ID].fbV;
        //ofstream outfile("/home/linaro/out.txt",ios::app);
        for(int kk=0;kk<output_length;kk++){
            //outfile << "kk: "<<kk<<"netout: "<< netop[kk] << endl;
            if(debug_disp){
                netop[kk] = back_25088[kk];
            }
            netout_f[kk] = ((float)netop[kk])/1000;
        }
        //outfile.close();
        boxes = interpret_output(netout_f, 14, 14, 448, 448, 720, 960, 0, 0, 4, 5, 0.2, 0.45, &numBoxes);

        qDebug()<<"generate box box_num: "<< numBoxes;
        if(DETECT_DANGER){
            cvRectangle(IM_show[shownum], cvPoint(0, 0), cvPoint(593, 719), cvScalar(0, 0, 255, 0), 3, 4, 0);
        }
        for (int i = 0; i < numBoxes; i++) {
            int x1 = (int)boxes[i].x1;
            int x2 = (int)boxes[i].x2;
            int y1 = (int)boxes[i].y1;
            int y2 = (int)boxes[i].y2;
            int type = (int)boxes[i].class_index;
            sprintf(name_and_prob, "%s:%.2f", vocnames[boxes[i].class_index], boxes[i].p);
            qDebug()<<"x1: "<< x1 << "x2: "<<x2<<"y1: "<<y1<<"y2: "<<y2<<"type: "<<name_and_prob;
            if(type == 1){ // man
                if(x1 < 593 & DETECT_DANGER){ //
                    res = 1;
                    cvRectangle(IM_show[shownum], cvPoint(x1, y1), cvPoint(x2, y2), cvScalar(0, 255, 255, 0), 3, 4, 0);
                    cvPutText(IM_show[shownum], "danger", cvPoint(x1, y1 - 10), &font, cvScalar(0, 0, 255, 0));
                    continue;
                }
                int hashat = 0;
                for(int j = 0; j < numBoxes; j++){
                    int type2 = (int)boxes[j].class_index;
                    if(type2 != 2)continue;
                    int xx1 = (int)boxes[j].x1;
                    int xx2 = (int)boxes[j].x2;
                    int yy1 = (int)boxes[j].y1;
                    int yy2 = (int)boxes[j].y2;
                    if(yy2 > y1 & xx2 > x1 & xx1 < x2 & yy1 < y2){
                        hashat = 1;
                        break;
                    }
                }
                if(!hashat){
                    res = 1;
                    cvRectangle(IM_show[shownum], cvPoint(x1, y1), cvPoint(x2, y2), cvScalar(0, 255, 255, 0), 3, 4, 0);
                    cvPutText(IM_show[shownum], "no hat", cvPoint(x1, y1 - 10), &font, cvScalar(0, 0, 255, 0));
                    continue;
                }
            }else if(type == 3){ //car
                car_high = 471500 - 837 * y1 - 29 * y2;
                sprintf(other_msg, "car_high:%.2d", car_high);
                if(car_high>236000){
                //if(car_high>156000){
                    res = 1;
                    cvRectangle(IM_show[shownum], cvPoint(x1, y1), cvPoint(x2, y2), cvScalar(0, 255, 255, 0), 3, 4, 0);
                }else{
                    cvRectangle(IM_show[shownum], cvPoint(x1, y1), cvPoint(x2, y2), cvScalar(0, 0, 255, 0), 3, 4, 0);
                }
                cvPutText(IM_show[shownum], other_msg, cvPoint(x1, y1 - 10), &font, cvScalar(0, 0, 255, 0));
                continue;
            }else if(type == 0){ //dog
                res = 1;
                cvPutText(IM_show[shownum], "dog", cvPoint(x1, y1 - 10), &font, cvScalar(255, 0, 0, 0));
                cvRectangle(IM_show[shownum], cvPoint(x1, y1), cvPoint(x2, y2), cvScalar(0, 255, 255, 0), 3, 4, 0);
                continue;
            }
            cvPutText(IM_show[shownum], name_and_prob, cvPoint(x1, y1 - 10), &font, cvScalar(255, 0, 0, 0));
            cvRectangle(IM_show[shownum], cvPoint(x1, y1), cvPoint(x2, y2), cvScalar(0, 0, 255, 0), 3, 4, 0);
        }

        free(boxes);
     }


    return res;

}




Thread_D::Thread_D()
{

}

void Thread_D::run()
{

    if(!init){
        cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX, 0.5, 0.5, 0, 1, 8);
        init = 1;
    }

    if(readstate_disp==0){


        //thread.start();

        readstate_disp=1;


        //qDebug()<<"transfer complete";

        unsigned int temp;
        int k = 0;
        const char* ch = "/home/linaro/conv9.txt";

        ifstream file_name(ch);
        if (!file_name.is_open())
        {
            qDebug() << "open file error!";
        }


        while (!file_name.eof())
        {
            file_name>>hex>>temp;
            back_25088[k] = (short)temp;

            if(k<2){
                qDebug()<<temp;
            }
            k++;

        }
        qDebug()<<k;
        file_name.close();

    }

    int res = camera_disp(shownum_temp);
    if(res==0){
        emit send(QString("every thing is ok"));
    }else if(res==1){
        emit send(QString("alert"));
    }else if(res==99){

    }else{
        emit send(QString("undefined error"));
    }
    qqimage[shownum_temp] = QImage((const uchar*)IM_show[shownum_temp]->imageData, IM_show[shownum_temp]->width, IM_show[shownum_temp]->height,QImage::Format_RGB32);
    emit sendN(shownum_temp);
    //qDebug()<<"display: "<< shownum_temp;

}

